import { _decorator, Component, Node, view, EventTarget, Button } from 'cc';
import { EventListener } from '../game/EventListener';
import { Events } from '../game/Events';
import super_html_playable from '../core/super_html_playable';
const { ccclass, property } = _decorator;

export enum Orientation {
    Landscape,  // 横屏
    Portrait    // 竖屏
}

@ccclass('UIManager')
export class UIManager extends Component {
    public static Instance: UIManager = null;
    public orientation: Orientation = Orientation.Portrait;

    @property(Button)
    private link1: Button = null!;
    @property(Button)
    private link2: Button = null!;

    onLoad() {
        UIManager.Instance = this;
        super_html_playable.game_ready();
        this.updateOrientation();
        view.on("canvas-resize", this.updateOrientation, this); //监听游戏尺寸变化
        this.link1.node.on(Button.EventType.CLICK, this.onLink, this);
        this.link2.node.on(Button.EventType.CLICK, this.onLink, this);
    }

    /**``           
     * 仅监听并赋值横竖屏状态
     */
    private updateOrientation() {
        const screenSize = view.getVisibleSize();
        const newOrientation = screenSize.width > screenSize.height ? Orientation.Landscape : Orientation.Portrait;
        if (this.orientation !== newOrientation) {
            this.orientation = newOrientation;
            EventListener.emit(Events.OrientationChange);
        }
    }

    private onLink() {
        super_html_playable.game_end();
        super_html_playable.download();
    }
}
